package pl.javarsovia.javarsovia4android.activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import pl.javarsovia.R;
import pl.javarsovia.javarsovia4android.domain.ConferenceProvider;
import pl.javarsovia.javarsovia4android.menu.commands.MapsButtonCommand;
import pl.javarsovia.javarsovia4android.menu.commands.PartnersButtonCommand;
import pl.javarsovia.javarsovia4android.menu.commands.SessionsButtonCommand;
import pl.javarsovia.javarsovia4android.menu.commands.SpeakersButtonCommand;
import pl.javarsovia.javarsovia4android.sync.SyncManager;
import pl.javarsovia.javarsovia4android.util.ImageManager;
import pl.javarsovia.javarsovia4android.util.UIEventsBinder;

import java.io.FileNotFoundException;

/**
 * @author mateusz.grzechocinski@gmail.com
 * @since 2010-05-27
 */
public class MenuActivity extends Activity {

	private UIEventsBinder binder;
	private SyncManager syncManager;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu);
		init();
	}

	public void init() {
		syncManager = new SyncManager(this);
		binder = new UIEventsBinder(this);
		binder.bindOnClickEvent(new MapsButtonCommand(this));

		loadAllPhotos();

		try {
			ConferenceProvider.readConferenceFromRaw(getApplicationContext());
			binder.bindOnClickEvent(new SessionsButtonCommand(this));
			binder.bindOnClickEvent(new SpeakersButtonCommand(this));
			//binder.bindOnClickEvent(new MySessionButtonCommand(this));
			binder.bindOnClickEvent(new PartnersButtonCommand(this));
		} catch (FileNotFoundException e) {
			showFirstUsageGuideDialog();
			return;
		}
	}

	private void showFirstUsageGuideDialog() {
		new AlertDialog.Builder(this)
				.setCancelable(true)
				.setIcon(android.R.drawable.ic_menu_info_details)
				.setTitle(R.string.first_run_title)
				.setMessage(R.string.first_run_guide)
				.setPositiveButton(R.string.update, syncManager)
				.setNeutralButton(R.string.not_now, null)
				.create()
				.show();
	}

	private void loadAllPhotos() {
		ImageManager.createInstanceFor(this).loadAllPhotos();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		final MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
		menu.findItem(R.id.menu_update).setOnMenuItemClickListener(syncManager);
		menu.findItem(R.id.menu_about).setIntent(new Intent(MenuActivity.this, AboutActivity.class));
		return super.onCreateOptionsMenu(menu);
	}
}
